from Tkinter import *
from OTabSettings import acc,bAcc
from tkFileDialog import asksaveasfilename
import OTFile

class TextWindow(Frame):
    def __init__(self, parent = None, text = ''):
        Frame.__init__(self, parent)
        self.master = parent
        self.menu = Menu(parent)
        parent.config(menu=self.menu)
        file = Menu(parent, tearoff=0)
        file.add_command(label='Save as...', command=self.onSave, 
            accelerator='%s+S' % acc, underline = 0)    
        file.add_separator()
        file.add_command(label='Close window', command=self.onDestroy, 
            accelerator='%s+W' % acc, underline=0)      
        self.menu.add_cascade(label='File', menu=file, underline = 0)

        scrollbar = Scrollbar(parent)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.text = Text(self, width=100, height=40, 
                 yscrollcommand=scrollbar.set)
        self.text.pack(side=LEFT, expand=YES, fill=BOTH)
        self.setText(text)

        self.master.bind('<%s-s>' % bAcc, self.onSave) 
        self.master.bind('<%s-w>' % bAcc, self.onDestroy) 
        
        scrollbar.config(command=self.text.yview)
        self.pack(expand=YES, fill=BOTH)


        
    def onDestroy(self, e=None):
        self.master.destroy()
        
    def setText(self, text):
        self.text.insert('1.0', text)
        
    def onSave(self, e=None):
        file = asksaveasfilename()
        text = self.text.get('1.0', END)
        OTFile.writeFile(file, text)
        
        
